package com.situation.controller;

import com.github.pagehelper.PageInfo;
import com.situation.annotation.ControllerEndpoint;
import com.situation.pojo.DO.TbRole;
import com.situation.pojo.DO.TbUser;
import com.situation.pojo.VO.MenuNodeVO;
import com.situation.pojo.VO.RoleVO;
import com.situation.service.RoleService;
import com.situation.service.impl.MenuServiceImpl;
import com.situation.util.ExcelUtil;
import com.situation.util.RespUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuServiceImpl menuService;

    @ControllerEndpoint(exceptionMessage = "添加角色失败", operation = "添加角色")
    @PostMapping("/add")
    @RequiresPermissions({"role:add"})
    public RespUtil add(@RequestBody RoleVO roleVO) {
        int i = roleService.add(roleVO);
        if (i > 0)
            return RespUtil.ok();
        return RespUtil.fail();
    }

    @ControllerEndpoint(exceptionMessage = "角色授权失败", operation = "角色授权")
    @PostMapping("/authority/{id}")
    @RequiresPermissions({"role:authority"})
    public RespUtil authority(@PathVariable Long id, Long[] mids) throws IllegalAccessException {
        int i = roleService.authority(id, mids);
        if (i == mids.length)
            return RespUtil.ok();
        return RespUtil.fail();
    }

    @ControllerEndpoint(exceptionMessage = "删除角色失败", operation = "删除角色")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions({"role:delete"})
    public RespUtil delete(@PathVariable Long id) throws IllegalAccessException {
        roleService.deleteById(id);
        return RespUtil.ok();
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions({"role:edit"})
    public RespUtil edit(@PathVariable Long id) throws IllegalAccessException {
        RoleVO edit = roleService.edit(id);
        return RespUtil.ok().setData(edit);
    }

    @ControllerEndpoint(exceptionMessage = "导出角色execl", operation = "导出角色execl")
    @GetMapping("/excel")
    @RequiresPermissions("role:export")
    public void excel(HttpServletResponse response) {
        List<TbRole> all = roleService.findAll();
        List<List<String>> excelData = new ArrayList<>();
        List<String> headList = new ArrayList<>();
        headList.add("ID");
        headList.add("角色名");
        headList.add("创建时间");
        headList.add("最近修改");
        headList.add("是否禁用");
        headList.add("备注");
        excelData.add(headList);
        for (TbRole tbRole : all) {
            List<String> list = new ArrayList<String>();
            list.add(tbRole.getId().toString());
            list.add(tbRole.getRoleName());
            list.add(tbRole.getCreateTime().toString());
            list.add(tbRole.getModifiedTime().toString());
            list.add(tbRole.getStatus() == 0 ? "是" : "否");
            list.add(tbRole.getRemark());
            excelData.add(list);
        }
        String sheetName = "角色列表";
        String fileName = "角色信息统计表";
        try {
            ExcelUtil.exportExcel(response, excelData, sheetName, fileName, 15);
        } catch (IOException e) {
            System.out.println("导出失败");
        }

    }

    @GetMapping("/findRoleList")
    public RespUtil findRoleList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                  RoleVO roleVO) {
        PageInfo<RoleVO> roleList = roleService.findRoleList(pageNum, pageSize, roleVO);
        return RespUtil.ok().setData(roleList);

    }

    @GetMapping("/findRoleMenu/{id}")
    public RespUtil findRoleMenu(@PathVariable Long id) throws IllegalAccessException {

        List<MenuNodeVO> tree = menuService.findMenuTree();
        //角色拥有的菜单id
        List<Long> mids = roleService.findMenuIdsByRoleId(id);
        List<Long> ids = menuService.findOpenIds();
        Map<String, Object> map = new HashMap<>();
        map.put("tree", tree);
        map.put("mids", mids);
        map.put("open", ids);
        return RespUtil.ok().setData(map);
    }

    @ControllerEndpoint(exceptionMessage = "更新角色失败", operation = "更新角色")
    @PutMapping("/update/{id}")
    @RequiresPermissions({"role:update"})
    public RespUtil update(@PathVariable Long id, @RequestBody RoleVO roleVO) throws IllegalAccessException {
        System.out.println(roleVO.getRemark());
        System.out.println(roleVO.getRoleName());
        int i = roleService.update(id, roleVO);
        if (i > 0) {
            return RespUtil.ok();
        }
        return RespUtil.fail();
    }

    @ControllerEndpoint(exceptionMessage = "更新状态失败", operation = "更新状态")
    @PutMapping("/updateStatus/{id}/{status}")
    @RequiresPermissions({"role:status"})
    public RespUtil updateStatus(@PathVariable("id") Long id, @PathVariable("status") Boolean status) throws IllegalAccessException {
        roleService.updateStatus(id, status);
        return RespUtil.ok();
    }
}
